package com.face.server.service.impl;

import com.face.server.dao.JoinedDAO;
import com.face.server.dao.VisitorDAO;
import com.face.server.entity.Admin;
import com.face.server.entity.Visitor;
import com.face.server.service.AdminVisitorService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ryan on 2018/3/19.
 */
@Service
public class AdminVisitorServiceImpl implements AdminVisitorService {
    @Autowired
    private JoinedDAO joinedDAO;

    @Autowired
    private VisitorDAO visitorDAO;

    @Override
    public Page<Visitor> listVisitors(Pageable pageable, String key) {//TODO
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin) subject.getPrincipal();
        List<Visitor> visitorList=new ArrayList<Visitor>();
        if (admin.isSuper()==true)
            visitorList=visitorDAO.listVisitors(pageable,key);
        else{
            List<Integer> dormitoryNumList=admin.getDormitoryNumList();
            visitorList=visitorDAO.listVisitors(pageable,key,dormitoryNumList);
        }
        return new PageImpl<Visitor>(visitorList,pageable,visitorList.size());
    }
}
